<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <style>
        table {
            width: 50%;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <table border="1" cellspacing='0' cellpadding='0'>
        <thead>
            <tr>
                <td>id</td>
                <td>姓名</td>
                <td>年龄</td>
                <td>性别</td>
                <td>爱好</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</body>
<script src="js/shuju.js"></script>
<script>
    window.onload = function() {
        var arr = JSON.parse(localStorage.arrobj)
        show(arr)
    }

    function show(arr) {
        var tbody = document.querySelector('tbody')
        tbody.innerHTML = '';
        var str = '';
        for (var i = 0; i < arr.length; i++) {
            str += `
                    <tr>
                        <td>${arr[i].myId}</td>
                        <td>${arr[i].myName} </td>
                        <td>${arr[i].myAge} </td>
                        <td>${arr[i].sex} </td>
                        <td>${arr[i].hobby} </td>
                        <td>
                            <button onclick="moveUp(${arr[i].myId})" class="up">上移</button>
                            <button onclick="del(${arr[i].myId})">删除</button>
                            <button onclick="moveDown(${arr[i].myId})">下移</button>
                        </td>
                    </tr>
            `
        }
        tbody.innerHTML = str;
    }
    //删除
    function del(id) {
        var arr = JSON.parse(localStorage.arrobj)
        for (var i = 0; i < arr.length; i++) {
            if (id == arr[i].myId) {
                arr.splice(i, 1);
            }
        }
        localStorage.arrobj = JSON.stringify(arr);
        show(arr);
    }
    //上移
    var up = document.querySelector('.up')

    function moveUp(id) {
        var arr = JSON.parse(localStorage.arrobj)
        for (var i = 0; i < arr.length; i++) {
            if (id == arr[i].myId) {
                if (i != 0) {
                    arr.splice(i - 1, 0, arr.splice(i, 1)[0]);
                    break;
                } else if (i == 0) {
                    disabled = 'none'
                    alert('不能上移');
                    break;
                }
            }
        }
        localStorage.arrobj = JSON.stringify(arr);
        show(arr);
    }
    //下移
    function moveDown(id) {
        var arr = JSON.parse(localStorage.arrobj)
        for (var i = 0; i < arr.length; i++) {
            if (id == arr[i].myId) {
                if (i != arr[i].length - 1) {
                    arr.splice(i + 1, 0, arr.splice(i, 1)[0]);
                    break;
                } else if (i == arr[i].length - 1) {
                    alert('不能下移');
                    break;
                }
            }
        }
        localStorage.arrobj = JSON.stringify(arr);
        show(arr);
    }
</script>

</html>